package com.se.contextMenu;

import com.se.common.Group;
import com.se.optionView.addRobotController;
import com.se.utils.ClientUtils;
import com.se.utils.ServiceUtils;
import com.se.utils.StageManager;
import com.se.utils.StageType;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;

public class GroupContextMenu {
    public static ContextMenu createGroupContextMenu(Group group) {
        ContextMenu contextMenu = new ContextMenu();

        MenuItem copyItem = new MenuItem("复制群ID");
        MenuItem addAIBotItem = new MenuItem("添加AI机器人");

        copyItem.setOnAction(e -> {
            Clipboard clipboard = Clipboard.getSystemClipboard();
            ClipboardContent clipboardContent = new ClipboardContent();
            clipboardContent.put(DataFormat.PLAIN_TEXT, String.valueOf(group.getGroupId()));
            clipboard.setContent(clipboardContent);
        });

        addAIBotItem.setOnAction(e -> addAI(group));

        contextMenu.getItems().addAll(copyItem);

        if((group.getGroupAI() == null || group.getGroupAI().isEmpty()) &&
                group.getGroupOwner().equals(ServiceUtils.getSpeaker().getUserName())) {
            contextMenu.getItems().add(addAIBotItem);
        }
        return contextMenu;
    }

    public static void addAI(Group group) {
        if(StageManager.get(StageType.ADD_ROBOT_STAGE) != null) {
            if(group == addRobotController.ADD_ROBOT_GROUP) {
                StageManager.get(StageType.ADD_ROBOT_STAGE).requestFocus();
                return;
            }
            else {
                StageManager.get(StageType.ADD_ROBOT_STAGE).close();
                StageManager.remove(StageType.ADD_ROBOT_STAGE);
            }
        }
        ClientUtils.showAddRobotPage();
        addRobotController.ADD_ROBOT_GROUP = group;
    }
}
